package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.SignInGet;
import com.longcai.piaojia.conn.SignInSubmitGet;
import com.longcai.piaojia.fragment.MineFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class SignInActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.sign_in_main_ll) private LinearLayout sign_in_main_ll;
    @BoundView(R.id.sign_in_ll) private LinearLayout sign_in_ll;
    @BoundView(R.id.sign_in_integration_title_tv) private TextView sign_in_integration_title_tv;
    @BoundView(R.id.sign_in_integration_tv) private TextView sign_in_integration_tv;
    @BoundView(R.id.sign_in_submit_tv) private TextView sign_in_submit_tv;
    @BoundView(R.id.sign_in_day_tv) private TextView sign_in_day_tv;
    private SignInGet.SignInEntity signIn;

    private SignInGet signInGet = new SignInGet(new AsyCallBack<SignInGet.SignInEntity>(){

        @Override
        public void onSuccess(String toast, int type, SignInGet.SignInEntity signInEntity) throws Exception {
            super.onSuccess(toast, type, signInEntity);

            signIn = signInEntity;

            if(signInEntity.state.equals("0")){

                sign_in_day_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_bg_color));
                sign_in_integration_title_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_bg_color));
                sign_in_integration_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_bg_color));
                sign_in_ll.setBackgroundResource(R.mipmap.bg_sign_in);
                sign_in_submit_tv.setBackgroundResource(R.drawable.sign_submit_bg);
                sign_in_submit_tv.setText(getResources().getString(R.string.sign_in_title_text));

            }else{

                sign_in_day_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
                sign_in_integration_title_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
                sign_in_integration_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
                sign_in_ll.setBackgroundResource(R.mipmap.bg_sign_in_sel);
                sign_in_submit_tv.setBackgroundResource(R.drawable.sign_submit_sel_bg);
                sign_in_submit_tv.setText(getResources().getString(R.string.sign_in_sel_title_text));

            }

            sign_in_main_ll.setVisibility(View.VISIBLE);

            sign_in_day_tv.setText(signInEntity.integral_continuous+getResources().getString(R.string.sign_in_day_unit_text));

            sign_in_integration_tv.setText(getResources().getString(R.string.sign_in_integration_title_text) + signInEntity.integral_month + getResources().getString(R.string.sign_in_integration_unit_text));

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(SignInActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    private SignInSubmitGet signInSubmitGet = new SignInSubmitGet(new AsyCallBack<List<String>>(){

        @Override
        public void onSuccess(String toast, int type, List<String> strings) throws Exception {
            super.onSuccess(toast, type, strings);

            sign_in_day_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
            sign_in_integration_title_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
            sign_in_integration_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sign_in_sel_bg_color));
            sign_in_ll.setBackgroundResource(R.mipmap.bg_sign_in_sel);
            sign_in_submit_tv.setBackgroundResource(R.drawable.sign_submit_sel_bg);
            sign_in_submit_tv.setText(getResources().getString(R.string.sign_in_sel_title_text));

            sign_in_day_tv.setText((Integer.parseInt(signIn.integral_continuous) + 1) + getResources().getString(R.string.sign_in_day_unit_text));

            sign_in_integration_tv.setText(getResources().getString(R.string.sign_in_integration_title_text) + (Integer.parseInt(signIn.integral_month)+Integer.parseInt(strings.get(0))) + getResources().getString(R.string.sign_in_integration_unit_text));

            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);
            getUserAdvert.refreshInfo();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(SignInActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sign_in);

        setBackTrue();

        setTitleName(getResources().getString(R.string.sign_in_title_text));

        sign_in_submit_tv.setOnClickListener(this);

        signInGet.user_id = BaseApplication.BasePreferences.readUserId();
        signInGet.execute(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.sign_in_submit_tv:

                signInSubmitGet.user_id = BaseApplication.BasePreferences.readUserId();
                signInSubmitGet.execute(SignInActivity.this);

                break;

        }

    }
}
